package auto.setting;

import auto.common.ImageConvertedBase64;
import auto.device.Phione;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//ps：只需要额外观察头像是否一致，以及切换二维码背景颜色是否变更即可

public class PrivateQRCode {

//feature1:检测设置-个人二维码是不是正常的
    public String privateQRCodeTest() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击设置页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]"))).click();

        String firstUser="**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        //如果第一个账号没有显示出来就向下滑，直到出现为止
        while(driver.findElements(AppiumBy.iOSClassChain(firstUser)).size()==0){
            Map<String, Object> argsDown = new HashMap<>();
            argsDown.put("direction", "down");
            driver.executeScript("mobile: swipe", argsDown);
        }

        //保存下来当前激活账号的用户名和昵称元素
        List<WebElement> nickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //保存两个元素的字符串
        String[] nickNameAndUSerNameArr=new String[2];
        nickNameAndUSerNameArr[0]=nickNameAndUSerName.get(0).getAttribute("value");
        nickNameAndUSerNameArr[1]=nickNameAndUSerName.get(1).getAttribute("value");

        //点击左上角的二维码
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("qrcoede"))).click();
        }catch(TimeoutException e){
            System.out.println("左上角查看个人二维码页面按钮消失");
        }

        //接受下面的检测方法返回的信息的
        String mark=null;

        //检测个人二维码的保存按钮是否正常
        mark=privateQrCodeSaveTest(wait);
        if(!mark.equals("正常")){
            System.out.println(mark);
            driver.quit();
            return  mark;
        }

        //检测二维码相关的
        mark=relatedToPrivateQrCodes(wait,nickNameAndUSerNameArr);
        if(!mark.equals("正常")){
            System.out.println(mark);
            driver.quit();
            return  mark;
        }

        //检测复制用户名按钮
        mark=userCopyButtonTest(wait);
        if(!mark.equals("正常")){
            System.out.println(mark);
            driver.quit();
            return  mark;
        }

        //二维码有效期按钮检查
        mark=qrCodeValidityTest(wait);
        if(!mark.equals("正常")){
            System.out.println(mark);
            driver.quit();
            return  mark;
        }

        //二维码背景颜色切换按钮检测
        mark=qrCodeBackdrop(wait);
        if(!mark.equals("正常")){
            System.out.println(mark);
            driver.quit();
            return  mark;
        }

        //分享到应用内按钮和分享到应用外按钮检测
        mark=shareButtonTest(wait);
        if(!mark.equals("正常")){
            System.out.println(mark);
            driver.quit();
            return  mark;
        }

        driver.quit();
        return "设置-个人二维码页面以及功能正常";
    }



//检测设置-个人二维码-保存按钮的
    private String privateQrCodeSaveTest(WebDriverWait wait) throws InterruptedException {
        String qrCodeSavePromptBase64= ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/qrCodeSavePrompt.png");

        //检测保存二维码按钮是否丢失
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("qrcode download"))).click();
        }catch(TimeoutException e){
            return "私人二维码页面保存按钮丢失";
        }
        Thread.sleep(500);
        //下载完之后如果跳转到设置页面就是正产的,跳转到设置页面还需要再次点击进入
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("qrcoede"))).click();
        }catch(TimeoutException e){//没有跳转就是保存失败
            return "点击保存按钮后无法进行跳转";
        }

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.image(qrCodeSavePromptBase64)));
            System.out.println("检测到保存成功提示");
        }catch (TimeoutException e){
            return "点击二维码保存按钮后没有出现提示";
        }

        System.out.println("二维码保存按钮能够正常点击，点击按钮页面能够正常跳转，程序运行完毕后请在相册中进行检查");
        return "正常";
    }


//检测设置-个人二维码-二维码相关信息的
    private String relatedToPrivateQrCodes(WebDriverWait wait,String[] nickNameAndUserNameArr){
        //先检查整个二维码的背景图是不是消失了
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                    "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeOther")));
        }catch(TimeoutException e){
            return "二维码页面背景图消失";
        }

        //检查二维码上方的头像是不是出现了
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                    "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeImage/XCUIElementTypeImage")));
            System.out.println("头像占位图正常出现，请观察二维码上方出现的头像是否和用户头像一致");
        }catch(TimeoutException e){
            return "二维码页面上方头像占位图没有存在";
        }

        //检测二维码页面昵称和用户名是否和用户列表中的相同     页面元素中第一个是有@的字符串看第二个是昵称
        String extractedString = ""; // 创建一个新的 String 变量来保存截取的内容

        // 查找 '@' 的位置
        int atIndex = nickNameAndUserNameArr[0].indexOf('@');

        // 检查是否找到了 '@' 并且它不在字符串的末尾
        if (atIndex != -1 && atIndex < nickNameAndUserNameArr[0].length() - 1) {
            // 从 '@' 开始截取字符串，并保存到新的变量中
            extractedString = nickNameAndUserNameArr[0].substring(atIndex); // 包含 '@' 字符
            System.out.println("截取后的字符串: " + extractedString);
        } else {
            System.out.println("字符串中不包含 '@' 或 '@' 后面没有内容。");
        }

        //检测二维码上方的昵称是不是和用户列表的一致或者消失
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId(extractedString)));
            System.out.println("用户名显示正常");
        }catch (TimeoutException e){
            return "二维码页面上面用户名未显示";
        }

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId(nickNameAndUserNameArr[1])));
            System.out.println("昵称显示正常");
        }catch (TimeoutException e){
            return "二维码页面上面昵称未显示";
        }

        //检测二维码是否正常出现
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeOther/XCUIElementTypeOther[3]")));
            System.out.println("二维码显示正常");
        }catch (TimeoutException e){
            return "二维码未显示";
        }

        return "正常";
    }

//检测设置-个人二维码-用户名复制按钮检测
    private String userCopyButtonTest(WebDriverWait wait){
        String copyUserNamePromptBase64= ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/copyUserNamePrompt.png");
        //点击这个复制按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon copy daytime"))).click();
            System.out.println("已成功点击复制按钮，请观察是否出现提示");
        }catch (TimeoutException e){
            return "复制用户名按钮消失";
        }

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.image(copyUserNamePromptBase64)));
            System.out.println("已检测到提示");
        }catch (TimeoutException e){
            return "提示未出现";
        }
        return "正常";
    }


//二维码有效期按钮检测
    private String qrCodeValidityTest(WebDriverWait wait){
        //点击设置时间的按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeOther/XCUIElementTypeOther[4]/XCUIElementTypeImage"))).click();
        }catch (TimeoutException e){
            return "设置时间的按钮丢失";
        }

        //查看点击设置有效期按钮后，是不是出现了时间列表，顺便点击取消
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("取消"))).click();
            System.out.println("出现设置有效期时间列表，取消按钮点击正常");
        }catch(TimeoutException e){
            return "设置到期时间的列表未弹出";
        }

        //再次点击设置时间按钮
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeOther/XCUIElementTypeOther[4]/XCUIElementTypeImage"))).click();

        //点击永久
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("永久"))).click();

        //点击完永久之后，查看设置的时间是不是永久了
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("有效期至: 永久")));
            System.out.println("有效期已经被设置为永久");
        }catch(TimeoutException e){
            return "有效期未被设置为永久";
        }

        return "正常";
    }


//二维码背景颜色变更按钮检测
    private String qrCodeBackdrop(WebDriverWait wait){
        //轮流点击四个
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeCollectionView/XCUIElementTypeCell[2]/XCUIElementTypeImage"))).click();
            System.out.println("已切换到第二个");

            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeCollectionView/XCUIElementTypeCell[3]/XCUIElementTypeImage"))).click();
            System.out.println("已切换到第三个");


            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeCollectionView/XCUIElementTypeCell[4]/XCUIElementTypeImage"))).click();
            System.out.println("已切换到第四个");
        }catch (TimeoutException e){
            return "背景色切换选项消失";
        }

        return "正常";
    }


//分享到应用内按钮和分享到应用外按钮检测
    private String shareButtonTest(WebDriverWait wait){
        //点击分享到外部的二维码
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"分享二维码\"`]"))).click();
            System.out.println("已点击分享到外部的按钮");
        }catch (TimeoutException e){
            return "分享到外部二维码丢失";
        }

        //判断这个弹窗是不是出现了
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeOther" +
                    "[`name == \"ActivityListView\"`]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView")));
            System.out.println("分享到外部的弹窗已经出现");
        }catch (TimeoutException e){
            return "分享到外部的弹窗没有出现";
        }

        //点击关闭的按钮
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("关闭"))).click();
        System.out.println("已关闭分享到外部的弹窗");

        //点击分享到内部的弹窗
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("emoji icon share contact"))).click();
            System.out.println("已点击分享到内部的按钮");
        }catch (TimeoutException e){
            return "分享到内部的弹窗按钮消失";
        }

        //判断分享到内部的弹窗是不是出现了
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther" +
                    "[3]/XCUIElementTypeOther[2]/XCUIElementTypeScrollView/XCUIElementTypeOther[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther")));
            System.out.println("分享到内部的页面已经出现");
        }catch (TimeoutException e){
            return "分享到内部的弹窗没有出现";
        }

        //出现之后点击取消随后关闭这个页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("OWSActionSheets.cancel"))).click();

        //关闭二维码页面进入设置页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("media composer close"))).click();

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")));
            System.out.println("成功返回设置页面");
        }catch(TimeoutException e){
            return "二维码关闭按钮点击无法使用";
        }

        return "正常";
    }

}
